using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;
namespace QLVMBAYDatacess
{
    public class LoaiGheDA : DataAccessBase
    {
		#region Constructors
		
		public LoaiGheDA() : base()
        {
			_selectSql = "Select * From LOAIGHE";
			_insertSql = "Insert Into LOAIGHE(MALOAIGHE,TENLOAIGHE,DIENGIAI) Values (@MALOAIGHE,@TENLOAIGHE,@DIENGIAI)";
        	_updateSql = "Update LOAIGHE Set TENLOAIGHE=@TENLOAIGHE,DIENGIAI=@DIENGIAI Where 1=1 And MALOAIGHE=@MALOAIGHE";
        	_deleteSql = "Delete From LOAIGHE Where 1=1 And MALOAIGHE=@MALOAIGHE";
        	_primarSql = "Select * From LOAIGHE Where 1=1 And MALOAIGHE=@MALOAIGHE";
        }
		
		protected static LoaiGheDA _inst = null;
		public static LoaiGheDA Inst
		{
			get
			{
				if(_inst == null)
					_inst = new LoaiGheDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private LoaiGhe GetCurrent(IDataReader reader, bool isDeep)
		{
			LoaiGhe ret = new LoaiGhe();
			ret.MaLoaiGhe = (string)reader["MALOAIGHE"];
			ret.TenLoaiGhe = (string)reader["TENLOAIGHE"];			
			ret.DienGiai = (string)reader["DIENGIAI"];
			
			
			return ret;
		}

		public List<LoaiGhe> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<LoaiGhe> ret = new List<LoaiGhe>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}
		
		public LoaiGhe GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MALOAIGHE", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}
		
		public bool Insert(LoaiGhe entity)
		{
			DbCommand cmd = GetCommand(_insertSql);
            SetParam(cmd, "@MALOAIGHE", entity.MaLoaiGhe);
            SetParam(cmd, "@TENLOAIGHE", entity.TenLoaiGhe);            
            SetParam(cmd, "@DIENGIAI", entity.DienGiai);

			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Update(LoaiGhe entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MALOAIGHE", entity.MaLoaiGhe);
            SetParam(cmd, "@TENLOAIGHE", entity.TenLoaiGhe);            
            SetParam(cmd, "@DIENGIAI", entity.DienGiai);

			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Delete(LoaiGhe entity)
		{
			DbCommand cmd = GetCommand(_deleteSql);
            SetParam(cmd, "@MALOAIGHE", entity.MaLoaiGhe);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		#endregion
    }
}


